/*
 * Copyright (C) 1997-2020 康成投资（中国）有限公司
 *
 * http://www.rt-mart.com
 *
 * 版权归本公司所有，不得私自使用、拷贝、修改、删除，否则视为侵权
 */
/*
 * com.zw.admin.server.component.impl
 * @author xiaoqun.liu
 * @date 2021年5月14日 下午7:37:02
 */
package com.zw.admin.server.component.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zw.admin.server.component.StockComponent;
import com.zw.admin.server.dao.PGoodsDao;
import com.zw.admin.server.dao.PGoodsStockDao;
import com.zw.admin.server.dao.PGoodsStockLogsDao;
import com.zw.admin.server.model.PGoods;
import com.zw.admin.server.model.PGoodsStock;
import com.zw.admin.server.model.PGoodsStockLogs;
import com.zw.admin.server.model.PPurchOrderItems;
import com.zw.admin.server.model.PSaleOrderItems;

/**
 *
 * @author xiaoqun.liu
 * @date 2021年5月14日 下午7:37:02
 */
@Component
public class StockComponentImpl implements StockComponent {

	@Autowired
	private PGoodsStockDao pGoodsStockDao;
	
	@Autowired
	private PGoodsStockLogsDao pGoodsStockLogsDao;
	

	@Autowired
	private PGoodsDao pGoodsDao;
	
	@Override
	public Integer saleStock(PSaleOrderItems pSaleOrderItems) {
		BigDecimal upStock = BigDecimal.ZERO.subtract(pSaleOrderItems.getQuantity() ); 
		return upStock(pSaleOrderItems.getSku() ,upStock);
	}

	@Override
	public Integer purchStock(List<PPurchOrderItems> listPPurchOrderItems) {
		for(PPurchOrderItems pPurchOrderItems : listPPurchOrderItems ) {
			BigDecimal upStock = pPurchOrderItems.getReceiveQty();
			return upStock(pPurchOrderItems.getSku() ,upStock);
		}
		
		return 1;
	}
	
	
	
	private Integer upStock(String sku,BigDecimal upStock) {
		List<PGoodsStock> stockList = pGoodsStockDao.listBySku(null,Arrays.asList(new String[] {sku} ));
		PGoodsStock pGoodsStock = new PGoodsStock();
		if(stockList==null ) {
			PGoods pGoods=pGoodsDao.getBySku(sku);
			pGoodsStock.setItemNo(pGoods.getItemNo());
			pGoodsStock.setGoodsName(pGoods.getGoodsName());
			pGoodsStock.setSku(pGoods.getSku());
			pGoodsStock.setUnit(pGoods.getUnit());
			pGoodsStockDao.insert(pGoodsStock);
		}else {
			pGoodsStock = stockList.get(0);
		}
		
		BigDecimal stock   = pGoodsStock.getStock().subtract(upStock);
		
		PGoodsStock uppGoodsStock = new  PGoodsStock();
		uppGoodsStock.setId(pGoodsStock.getId());
		uppGoodsStock.setStock(stock);
		
		PGoodsStockLogs pGoodsStockLogs = new PGoodsStockLogs();
		pGoodsStockLogs.setItemNo(pGoodsStock.getItemNo());
		pGoodsStockLogs.setSku(pGoodsStock.getSku());
		pGoodsStockLogs.setStock( stock);
		pGoodsStockLogs.setUpdateStock(upStock);
		pGoodsStockLogsDao.insert(pGoodsStockLogs);
		return 1;
	}
	
	@Override
	public Integer upStock(Integer id,BigDecimal upStock) {
		PGoodsStock  pGoodsStock = pGoodsStockDao.getById((long)id);
		
		BigDecimal stock   = pGoodsStock.getStock().subtract(upStock);
		
		PGoodsStock uppGoodsStock = new  PGoodsStock();
		uppGoodsStock.setId(pGoodsStock.getId());
		uppGoodsStock.setStock(stock);
		
		PGoodsStockLogs pGoodsStockLogs = new PGoodsStockLogs();
		pGoodsStockLogs.setItemNo(pGoodsStock.getItemNo());
		pGoodsStockLogs.setSku(pGoodsStock.getSku());
		pGoodsStockLogs.setStock( stock);
		pGoodsStockLogs.setUpdateStock(upStock);
		pGoodsStockLogsDao.insert(pGoodsStockLogs);
		return 1;
	}

	

}
